package cn.glinks.liuhe.utils;

import android.content.Context;
import android.graphics.Point;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.ScaleAnimation;
import com.amap.api.maps.model.animation.TranslateAnimation;

import cn.glinks.liuhe.R;

import static com.amap.api.maps.model.BitmapDescriptorFactory.fromBitmap;


/**
 * 地图Marker集合工具类
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-03 12:30
 */

public class MapMarkerUtils {
    public static BitmapDescriptor sGreenPoint_6;
    public static BitmapDescriptor sGreenPoint_8;
    public static BitmapDescriptor sGreenPoint_10;
    public static BitmapDescriptor sGreenPoint_12;
    public static BitmapDescriptor sGreenPoint_14;
    public static BitmapDescriptor sRedPoint_6;
    public static BitmapDescriptor sRedPoint_8;
    public static BitmapDescriptor sRedPoint_10;
    public static BitmapDescriptor sRedPoint_12;
    public static BitmapDescriptor sRedPoint_14;

    /**
     * 加载适合大小的Marker图标
     */
    public static void resizeMarkIcon(Context context) {
//        sUnqualifiedMarkerBitDes =
//                fromBitmap(ImageUtils.zoomBitmap(
//                        BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_unqualified)
//                        ,DisplayUtils.dip2px(15),DisplayUtils.dip2px(15)));
//        sQualifiedMarkerBitDes =
//                fromBitmap(ImageUtils.zoomBitmap(
//                        BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_qualified)
//                        ,DisplayUtils.dip2px(15),DisplayUtils.dip2px(15)));

        sGreenPoint_6 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_green_circle_6));
        sGreenPoint_8 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_green_circle_8));
        sGreenPoint_10 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_green_circle_10));
        sGreenPoint_12 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_green_circle_12));
        sGreenPoint_14 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_green_circle_14));
        sRedPoint_6 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_red_circle_6));
        sRedPoint_8 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_red_circle_8));
        sRedPoint_10 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_red_circle_10));
        sRedPoint_12 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_red_circle_12));
        sRedPoint_14 = fromBitmap(ImageUtils.drawableToBitmap(context, R.drawable.map_red_circle_14));

    }


    /**
     * 对指定marker启动跳动动画
     * @param aMap AMap 地图对象
     * @param marker Marker Marker对象
     */
    public static void startJumpAnimation(AMap aMap, Marker marker) {
        if (null != aMap && null != marker ) {
            //根据屏幕距离计算需要移动的目标点
            final LatLng latLng = marker.getPosition();
            Point point =  aMap.getProjection().toScreenLocation(latLng);
            point.y -= DisplayUtils.dip2px(50);
            LatLng target = aMap.getProjection().fromScreenLocation(point);
            //使用TranslateAnimation,填写一个需要移动的目标点
            Animation animation = new TranslateAnimation(target);
            animation.setInterpolator(new Interpolator() {
                @Override
                public float getInterpolation(float input) {
                    // 模拟重加速度的interpolator
                    if(input <= 0.5) {
                        return (float) (0.5f - 2 * (0.5 - input) * (0.5 - input));
                    } else {
                        return (float) (0.5f - Math.sqrt((input - 0.5f)*(1.5f - input)));
                    }
                }
            });
            //整个移动所需要的时间
            animation.setDuration(600);
            //设置动画
            marker.setAnimation(animation);
            //开始动画
            marker.startAnimation();
        } else {
            LogUtils.loge("aMap or marker is null");
        }
    }

    /**
     * 对指定marker启动生长动画
     * @param aMap AMap 地图对象
     * @param marker Marker Marker对象
     */
    public static void startGrowAnimation(AMap aMap, Marker marker) {
        if (null != aMap && null != marker ) {
            Animation animation = new ScaleAnimation(0,1,0,1);
            animation.setInterpolator(new LinearInterpolator());
            animation.setDuration(600);//整个移动所需要的时间
            marker.setAnimation(animation);//设置动画
            marker.startAnimation();//开始动画
        } else {
            LogUtils.loge("aMap or marker is null");
        }
    }

    /**
     * 获取合格情况指示Marker的MarkerOptions
     *
     * @param title
     * @param latLng LatLng
     * @return MarkerOptions
     */
    public static MarkerOptions getQualifiedMarkerOptionsFrom(String title, LatLng latLng) {
        MarkerOptions markerOptions = new MarkerOptions()
                .title(title)
                .infoWindowEnable(false)
                .draggable(false)
                .icon(sGreenPoint_8)
                .anchor(1.0f,0.5f)
                .rotateAngle(0)
                .position(latLng);

        return markerOptions;
    }

    /**
     * 获取不合格情况指示Marker的MarkerOptions
     *
     * @param title
     * @param latLng LatLng
     * @return MarkerOptions
     */
    public static MarkerOptions getUnqualifiedMarkerOptionsFrom(String title, LatLng latLng) {
        MarkerOptions markerOptions = new MarkerOptions()
                .title(title)
                .infoWindowEnable(false)
                .draggable(false)
                .icon(sRedPoint_8)
                .anchor(1.0f,0.5f)
                .rotateAngle(0)
                .position(latLng);

        return markerOptions;
    }

}
